home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / comm / tcp / Amster-source.lha / Amster_Install / Source / channellist.c < prev    next >
C/C++ Source or Header  |  2001-03-14  |  7KB  |  237 lines

  1. /*
  2. ** Amster - Channel List
  3. ** Copyright © 2000-2001 by Jacob Laursen
  4. **
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation; either version 2 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include "amster.h"
  21.  
  22. #include <proto/dos.h>
  23. #include <proto/utility.h>
  24.  
  25. #include <MUI/NListview_mcc.h>
  26.  
  27. #include "amster_Cat.h"
  28.  
  29. /* Private prototypes */
  30.  
  31. MUI_LIST_DISP_DECL(ChannelListDisplay, struct ChannelEntry *entry);
  32. MUI_NLIST_COMP_DECL(ChannelListCompare);
  33. MUI_LIST_DEST_DECL(ChannelListDestruct, struct ChannelEntry *entry);
  34.  
  35.  
  36. MUI_DISPATCH(ChannelListDispatch)
  37. {
  38.     struct ChannelListData *data;
  39.  
  40.     switch (msg->MethodID) {
  41.         case OM_NEW:
  42.             return(ChannelListNew(cl, obj, (APTR)msg));
  43.         case CHANLIST_UPDATE:
  44.             {
  45.             u_long item;
  46.             data = INST_DATA(cl, obj);
  47.  
  48.             set(data->LV_Channel, MUIA_NList_Quiet, MUIV_NList_Quiet_Visual);
  49.             while (1) {
  50.                 DoMethod(data->LV_Channel, MUIM_NList_GetEntry, 0, &item);
  51.                 if (!item) break;
  52.                 DoMethod(data->LV_Channel, MUIM_NList_Remove, MUIV_NList_Remove_First);
  53.             }
  54.             set(data->LV_Channel, MUIA_NList_Quiet, MUIV_NList_Quiet_None);
  55.             nap_sendbuf(NAPC_LIST_CHANNELS, "");
  56.             break;
  57.             }
  58.         case CHANLIST_JOIN:
  59.             {
  60. /*              channel c = NULL;*/
  61.             struct ChannelEntry *entry;
  62.             Object *win;
  63.             data = INST_DATA(cl, obj);
  64.  
  65.             DoMethod(data->LV_Channel, MUIM_NList_GetEntry, MUIV_NList_GetEntry_Active, &entry);
  66.             if (entry) {
  67. /*                if (chat_chans->name) c = chat_findchan(entry->Name);
  68.                 if (!c) {*/
  69. #ifdef AMSTER_DEBUG
  70. gui_debug("join it!");
  71. #endif
  72.                     win = NewObject(gui->chat_mcc->mcc_Class, NULL, TAG_DONE);
  73.                     if (!win) return NULL;
  74.                     DoMethod(gui->app, OM_ADDMEMBER, win);
  75.                     set(win, MUIA_Window_Open, TRUE);
  76.                     DoMethod(win, CHAT_JOINCHANNEL, entry->Name);
  77. /*                }
  78.                 else {
  79. #ifdef AMSTER_DEBUG
  80. gui_debug("already joined");
  81. #endif
  82.                 }*/
  83.             }
  84.             return NULL;
  85.             }
  86.         case CHANLIST_ENTRY:
  87.             {
  88.             struct ChannelEntry *entry;
  89.             data = INST_DATA(cl, obj);
  90.  
  91.             if (entry = malloc(sizeof(struct ChannelEntry))) {
  92.                 entry->Name = strdup((char *)(((muimsg)msg)->arg1));
  93.                 entry->NumUsers = (int)(((muimsg)msg)->arg2);
  94.                 entry->Topic = strdup((char *)(((muimsg)msg)->arg3));
  95.                 DoMethod(data->LV_Channel, MUIM_NList_InsertSingle, entry, MUIV_NList_Insert_Sorted);
  96.             }
  97.             break;
  98.             }
  99.     }
  100.     return(DoSuperMethodA(cl, obj, msg));
  101. }
  102.  
  103.  
  104. ULONG ChannelListNew(struct IClass *cl, Object *obj, struct opSet *msg)
  105. {
  106.     static const struct Hook ChannelListDispHook = { {NULL, NULL}, &ChannelListDisplay,  NULL, NULL };
  107.     static const struct Hook ChannelListCompHook = { {NULL, NULL}, &ChannelListCompare,  NULL, NULL };
  108.     static const struct Hook ChannelListDestHook = { {NULL, NULL}, &ChannelListDestruct, NULL, NULL };
  109.  
  110.     struct ChannelListData *data;
  111.  
  112.     Object *LV_Channel;
  113.     Object *BT_Update, *BT_Join;
  114.  
  115.     if (obj = (Object *)DoSuperNew(cl, obj,
  116.         MUIA_HelpNode, "channellist",
  117.         MUIA_Window_Title, MSG_CHANNELLIST_TITLE,
  118.         MUIA_Window_ID, MAKE_ID('C','H','L','S'),
  119.         WindowContents, VGroup,
  120.             Child, LV_Channel = NListviewObject,
  121.                 MUIA_NList_Input, TRUE,
  122.                 MUIA_NListview_NList, NListObject,
  123.                     InputListFrame,
  124.                     MUIA_NList_ListBackground, MUII_ListBack,
  125.                     MUIA_NList_Title, TRUE,
  126.                     MUIA_NList_Format, "BAR,BAR,",
  127.                     MUIA_NList_DisplayHook, &ChannelListDispHook,
  128.                     MUIA_NList_CompareHook2, &ChannelListCompHook,
  129.                     MUIA_NList_DestructHook, &ChannelListDestHook,
  130.                 End,
  131.             End,
  132.             Child, HGroup,
  133.                 Child, BT_Join   = SimpleButton(MSG_CHANNELLIST_JOIN_GAD),
  134.                 Child, BT_Update = SimpleButton(MSG_CHANNELLIST_UPDATE_GAD),
  135.             End,
  136.         End,
  137.         TAG_MORE, msg->ops_AttrList))
  138.     {
  139.         data = INST_DATA(cl, obj);
  140.         data->LV_Channel  = LV_Channel;
  141.  
  142.         DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  143.  
  144.         DoMethod(BT_Update, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, CHANLIST_UPDATE);
  145.         DoMethod(BT_Join,   MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, CHANLIST_JOIN);
  146.  
  147.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_TitleClick,  MUIV_EveryTime, LV_Channel, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_Both);
  148.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_TitleClick2, MUIV_EveryTime, LV_Channel, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_2);
  149.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_SortType,    MUIV_EveryTime, LV_Channel, 3, MUIM_Set, MUIA_NList_TitleMark,  MUIV_TriggerValue);
  150.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_SortType2,   MUIV_EveryTime, LV_Channel, 3, MUIM_Set, MUIA_NList_TitleMark2, MUIV_TriggerValue);
  151.  
  152.         return((ULONG)obj);
  153.     }
  154.     return NULL;
  155. }
  156.  
  157.  
  158. MUI_LIST_DISP(ChannelListDisplay, struct ChannelEntry *entry)
  159. {
  160.     static char users[5];
  161.  
  162.     if (entry) {
  163.         *array++ = entry->Name;
  164.         sprintf(users, "%d", entry->NumUsers);
  165.         *array++ = users;
  166.         *array = entry->Topic;
  167.     }
  168.     else {
  169.         *array++ = (char *)MSG_CHANNELLIST_NAME;
  170.         *array++ = (char *)MSG_CHANNELLIST_USERS;
  171.         *array   = (char *)MSG_CHANNELLIST_TOPIC;
  172.     }
  173.  
  174.     return NULL;
  175. }
  176.  
  177.  
  178. MUI_NLIST_COMP(ChannelListCompare)
  179. {
  180.     struct ChannelEntry *entry1 = ncm->entry1;
  181.     struct ChannelEntry *entry2 = ncm->entry2;
  182.     LONG col1 = ncm->sort_type & MUIV_NList_TitleMark_ColMask;
  183.     LONG col2 = ncm->sort_type2 & MUIV_NList_TitleMark2_ColMask;
  184.     ULONG result = 0;
  185.  
  186.     if (ncm->sort_type == MUIV_NList_SortType_None) return (0);
  187.  
  188.     if (col1 == 0) {
  189.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  190.             result = (LONG) stricmp(entry2->Name, entry1->Name);
  191.         else
  192.             result = (LONG) stricmp(entry1->Name, entry2->Name);
  193.     }
  194.     else if (col1 == 1) {
  195.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  196.             result = entry2->NumUsers - entry1->NumUsers;
  197.         else
  198.             result = entry1->NumUsers - entry2->NumUsers;
  199.     }
  200.     else if (col1 == 2) {
  201.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  202.             result = (LONG) stricmp(entry2->Topic, entry1->Topic);
  203.         else
  204.             result = (LONG) stricmp(entry1->Topic, entry2->Topic);
  205.     }
  206.  
  207.     if ((result != 0) || (col1 == col2)) return (result);
  208.  
  209.     if (col2 == 0) {
  210.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  211.             result = (LONG) stricmp(entry2->Name, entry1->Name);
  212.         else
  213.             result = (LONG) stricmp(entry1->Name, entry2->Name);
  214.     }
  215.     else if (col2 == 1) {
  216.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  217.             result = entry2->NumUsers - entry1->NumUsers;
  218.         else
  219.             result = entry1->NumUsers - entry2->NumUsers;
  220.     }
  221.     else if (col2 == 2) {
  222.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  223.             result = (LONG) stricmp(entry2->Topic, entry1->Topic);
  224.         else
  225.             result = (LONG) stricmp(entry1->Topic, entry2->Topic);
  226.     }
  227.  
  228.     return (result);
  229. }
  230.  
  231.  
  232. MUI_LIST_DEST(ChannelListDestruct, struct ChannelEntry *entry)
  233. {
  234.     free(entry);
  235.     return NULL;
  236. }
  237.